package com.faxe.educar.dash.controller;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.faxe.educar.ws.bo.UserBO;
import com.faxe.educar.ws.to.UserTO;

@Controller
public class GlobalControllerDash {

	UserBO mgrUser;
	
	@RequestMapping(value="/dash", method = RequestMethod.GET)
	public ModelAndView viewHome(HttpServletRequest request, Principal principal) {

		if (principal == null){
			return new ModelAndView("redirect:/");
		}
		
		if (request.getSession().getAttribute("USER") == null){
			UserTO user = mgrUser.getUserByEmail(principal.getName());
			request.getSession().setAttribute("USER", user);
		}
		
		return new ModelAndView("dash/home");
	}
	
	@RequestMapping(value="/newService", method = RequestMethod.GET)
	public ModelAndView viewNewService() {

		return new ModelAndView("dash/newService");
	}
	
	@Autowired
	public void setUserBO(UserBO userBO){
		this.mgrUser = userBO;
	}
}
